<template>
  <div class="page">
    <el-form :model="searchForm" label-position="left" inline label-suffix="：" size="small">
      <div class="flex wap flex-start">
        <el-form-item label="商品类别">
          <checked-cate :itemId="searchForm.producttypeid" @checkedItem="checkedCate"></checked-cate>
        </el-form-item>
        <el-form-item label="商品名称">
          <el-input v-model="searchForm.productname"></el-input>
        </el-form-item>
        <el-form-item label="">
          <el-button type="primary" @click="getData">查询</el-button>
        </el-form-item>
      </div>
    </el-form>
    <div class="flex wap flex-start" style="padding: 0px 0 10px 0">
      <el-button type="success" @click="addPro">新增商品</el-button>
    </div>
    <el-table :data="tableData" border style="width: 100%"  height="600"  :header-cell-style="{
        background: '#eef1f6',
        color: '#606266',
        'text-align': 'center'
      }"
      :cell-style="{ 'text-align': 'center' }" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection"></el-table-column> -->
      <!-- <el-table-column prop="productid" label="商品编号"></el-table-column> -->
      <el-table-column prop="producttypename" label="商品类别"></el-table-column>
      <el-table-column prop="productname" label="商品名称"></el-table-column>
      <el-table-column prop="oldprice" label="原价"></el-table-column>
      <el-table-column prop="newprice" label="优惠价"></el-table-column>
      <el-table-column prop="createtime" label="创建时间"></el-table-column>
      <el-table-column prop="mainimg" label="商品图片">
        <template slot-scope="scop">
          <el-image fit="fil" style="width: 80px; height: 50px" :src="$Url + '/upload/' + scop.row.mainimg"
            :preview-src-list="[$Url + '/upload/' + scop.row.img1,$Url + '/upload/' + scop.row.img2,$Url + '/upload/' + scop.row.img3]">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column prop="sale" label="上架状态">
        <template slot-scope="scop">
          <el-tag size="mini" type='success' v-if="scop.row.sale == 1">已上架</el-tag>
          <el-tag size="mini" type="danger" v-else>未上架</el-tag>
        </template>
      </el-table-column>
      
      <!-- <el-table-column prop="productinfo" label="商品简介" width="300" show-overflow-tooltip></el-table-column> -->
      <el-table-column prop="" label="操作" width="180">
        <template slot-scope="scop">
          <el-button type="primary" icon="el-icon-edit" circle @click="edit(scop.row)"></el-button>
          <el-button type="warning" icon="el-icon-view" circle @click="lookInfo(scop.row)"></el-button>
          <el-button type="danger" icon="el-icon-delete" circle @click="del(scop.row.productid)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="float:right;padding:10px">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :page-sizes="[10, 20, 50, 100, 200]"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>
    <el-dialog title="商品详情" center :visible.sync="detailVisible" width="30%">
      <div style="overflow:auto;max-height:500px">
        <div style="padding:10px 0">{{proInfo.productinfo}}</div>
        <el-image fit="fil" style="width: 100%" :src="$Url + '/upload/' + proInfo.imgdetails"></el-image>
        <!-- <el-image fit="fil" style="width: 100%" src="https://fuss10.elemecdn.com/3/28/bbf893f792f03a54408b3b7a7ebf0jpeg.jpeg"></el-image> -->
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="detailVisible = false">返  回</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="isEdit?'修改商品':'新增商品'" center :visible.sync="dialogFormVisible" width="40%">
      <el-form :model="addOrEditForm" ref="addOrEditPro" :rules="rules" label-width="100px" class="edit-form">
        <el-form-item label="商品分类：" prop="producttypeid">
          <checked-cate :itemId="addOrEditForm.producttypeid" @checkedItem="addCheckedCate"></checked-cate>
        </el-form-item>
        <el-form-item label="商品名称：" prop="productname">
          <el-input type="text" placeholder="请输入商品名称" v-model="addOrEditForm.productname" clearable></el-input>
        </el-form-item>
        <el-form-item label="商品简介：" prop="productinfo">
          <el-input type="textarea" :autosize="{ minRows: 2 }"  placeholder="请输入商品简介" v-model="addOrEditForm.productinfo"></el-input>
        </el-form-item>
        <div class="pic-box flex wape flex-between">
          <el-form-item label="封面图：" prop="mainimg">
            <upload v-model="addOrEditForm.mainimg"/>
          </el-form-item>
          <el-form-item label="详情图1：" prop="img1">
            <upload v-model="addOrEditForm.img1"/>
          </el-form-item>
          <el-form-item label="详情图2：" prop="img2">
            <upload v-model="addOrEditForm.img2"/>
          </el-form-item>
          <el-form-item label="详情图3：" prop="img3">
            <upload v-model="addOrEditForm.img3"/>
          </el-form-item>
          <el-form-item label="细节图：" prop="img3">
            <upload v-model="addOrEditForm.imgdetails"/>
          </el-form-item>
        </div>
        <el-form-item label="商品原价：" prop="oldprice">
          <el-input type="text" placeholder="请输入商品原价" v-model="addOrEditForm.oldprice" clearable></el-input>
        </el-form-item>
        <el-form-item label="优惠价：" prop="newprice">
          <el-input type="text" placeholder="请输入商品优惠价" v-model="addOrEditForm.newprice" clearable></el-input>
        </el-form-item>
        <el-form-item label="是否上架：" prop="sale">
          <el-switch v-model="addOrEditForm.sale" :active-value="1" :inactive-value="2" active-text="已上架" inactive-text="未上架"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveAddOrUpdata">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { findProduct, updateProduct, delProduct, addProduct } from "@/api/user";
import checkedCate from "@/views/components/checkedCate"
import upload from "@/components/uploadFile";
export default {
  components: {
    checkedCate,
    upload
  },
  data() {
    return {
      total: 0,
      pageNum: 1,
      pageSize: 10,
      isEdit:false,
      tableData: [],
      checkItemIds: [],
      paymentids: [],
      dialogFormVisible: false,
      detailVisible:false,
      proInfo: {},
      dateTime: '',
      searchForm: {
        producttypeid: '',
        productname: ''
      },
      addOrEditForm: {
        producttypeid: '',
        productname: '',
        productinfo: '',
        mainimg: '',
        img1: '',
        img2: '',
        img3: '',
        imgdetails:'',
        oldprice: '',
        newprice: '',
        sale: 2
      },
      rules: {
        producttypeid: [
          { required: true, message: '必须选择商品分类', trigger: 'change' },
        ],
        productname: [
          { required: true, message: '必须填写商品名称', trigger: 'change' },
          { min: 2, max: 15, message: '长度在 2 到 15 个字符', trigger: 'change' }
        ],
        productinfo: [
          { required: true, message: '必须填写商品简介', trigger: 'change' },
        ],
        oldprice: [
          { required: true, message: '必须填写商品原价', trigger: 'change' },
          { pattern: /^\d+(\.\d+)?$/, message: '价格必须为数字值' }
        ],
        newprice: [
          { required: true, message: '必须填写商品优惠价', trigger: 'change' },
          { pattern: /^\d+(\.\d+)?$/, message: '价格必须为数字值' }
        ]
      },
    }
  },
  computed: {
    queryData() {
      return {
        producttypeid: this.searchForm.producttypeid,
        productname: this.searchForm.productname,
        pageNum: this.pageNum,
        pageSize: this.pageSize
      };
    }
  },
  mounted() {
    this.getData();
  },
  methods: {
    async getData() {
      const res = await findProduct.post(this.queryData);
      if (res.resultCode === "000000") {
        this.tableData = res.data.productList;
        this.total = res.data.count;
      } else {
        this.$message.error(res.resultInfo);
      }
    },
    lookInfo(row){
      this.proInfo = row
      this.detailVisible = true
    },
    handleSelectionChange(val) {
      // if (val.length > 0) {
      //   this.checkItemIds = val.map(e => e.paymentid)
      // }
    },
    checkedCate(val) {
      this.searchForm.producttypeid = val
    },
    addCheckedCate(val) {
      this.addOrEditForm.producttypeid = val
    },
    addPro() {
      this.isEdit = false
      this.dialogFormVisible = true
      this.addOrEditForm = {
        producttypeid: '',
        productname: '',
        productinfo: '',
        mainimg: '',
        img1: '',
        img2: '',
        img3: '',
        imgdetails:'',
        oldprice: '',
        newprice: '',
        sale: 2
      }
    },
    edit(row) {
      this.isEdit = true
      this.dialogFormVisible = true
      const { productid, producttypeid, productname, productinfo, mainimg, img1, img2, img3, imgdetails, oldprice, newprice, sale } = row
      this.addOrEditForm = {
        productid,
        producttypeid,
        productname,
        productinfo,
        mainimg,
        img1,
        img2,
        img3,
        imgdetails,
        oldprice,
        newprice,
        sale
      }
    },
    saveAddOrUpdata() {
      this.$refs['addOrEditPro'].validate((valid) => {
        if (valid) {
          this.AddOrUpdata()
        } else {
          return false;
        }
      });
    },
    async AddOrUpdata() {
      if (this.isEdit) {
        const res = await updateProduct.post(this.addOrEditForm);
        if (res.resultCode === "000000") {
          this.$message.success(res.resultInfo);
          this.dialogFormVisible = false
          this.getData()
        } else {
          this.$message.error(res.resultInfo);
        }
      } else {
        const res = await addProduct.post(this.addOrEditForm);
        if (res.resultCode === "000000") {
          this.$message.success(res.resultInfo);
          this.dialogFormVisible = false
          this.getData()
        } else {
          this.$message.error(res.resultInfo);
        }
      }
    },
    del(id) {
      this.$confirm("此操作将删除该项目, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        delProduct.post({ productid: id }).then(res => {
          if (res.resultCode === "000000") {
            this.$message({
              type: "success",
              message: "删除成功!"
            })
            this.getData()
          } else {
            this.$message.error(res.resultInfo);
          }
        })
      }).catch(() => {
        this.$message({
          type: "info",
          message: "已取消删除"
        });
      });
    },
    handleSizeChange(val) {
      this.pageNum = 1;
      this.pageSize = val;
      this.getData();
    },
    handleCurrentChange(val) {
      this.pageNum = val;
      this.getData();
    }
  }
};
</script>
<style lang="less" scoped></style>
